import { ListItem } from "../utils/Type"
const initialState = {//定义数据
    list: [],
    shopList: [],
    tabs: [],
    detailInfo: {},
    allChecked: false,
    sum: 0,
}

// 计算总价
function getSum(list: Array<ListItem>) {
    let sum = 0
    list.forEach(item => {
        if (item.checked) {
            sum += item.price * item.num//总价=数量*价格
        }
    })
    return sum//最好的返回值
}

let reducer = (state = initialState, { type, payload }: any) => {
    let newState = JSON.parse(JSON.stringify(state))
    switch (type) {

        case "LIST"://list请求
            if (payload.pageSize === 1) {//判断页码
                newState.list = payload.data
            } else {
                newState.list.push(...payload.data)
            }
            return newState
        case "ADD"://增加
            newState.list.forEach((item: ListItem) => {
                if (item.id === payload) {
                    item.num++
                    newState.shopList = newState.list.filter((item: ListItem) => {
                        return item.num
                    })
                }
            })
            return newState
        case "LOW"://减少
            newState.list.forEach((item: ListItem) => {
                if (item.id === payload) {
                    item.num--
                }
            })
            // 寻找下标
            let index = newState.shopList.findIndex((item: ListItem) => {
                return item.id === payload
            })
            console.log(index)
            newState.shopList.filter((item: ListItem) => {
                if (item.id === payload) {
                    if (item.num === 1) {
                        newState.shopList.splice(index, 1)//进行删除
                    } else {
                        item.num--
                    }
                }
                return item
            })
            return newState

        // tab请求
        case 'GET_TABS':
            newState.tabs = payload
            return newState
        // 详情
        case 'GET_DETAIL':
            const filterDetailList = newState.list.filter((item: ListItem) => item.id === payload)
            if (filterDetailList.length) {
                newState.detailInfo = filterDetailList[0] || {}
            }
            return newState
        // 单选
        case 'CHECKED_ITEM':
            const indexs = newState.shopList.findIndex((item: ListItem) => item.id === payload)
            if (indexs !== -1) {
                newState.shopList[indexs].checked = !newState.shopList[indexs].checked//check取反

                newState.allChecked = newState.shopList.every((item: ListItem) => item.checked)//反选
                newState.sum = getSum(newState.shopList)//总价
            }
            return newState
        // 全选
        case 'CHANGE_ALL':
            newState.shopList.forEach((item: ListItem) => {//全选
                item.checked = payload
            })
            newState.allChecked = payload//改变allChecked的值
            newState.sum = getSum(newState.shopList)//总价
            return newState
        default:
            return newState
    }
}

export default reducer
